// 封装song对象
export default class Song {
  constructor({ id, name, author, url, picUrl, duration }) {
    this.id = id;
    this.name = name;
    this.author = author;
    this.duration = duration;
    this.url = url;
    this.picUrl = picUrl;
  }
}

function getAuthor(options) {
  let author = '';
  if (options.ar) {
    author = options.ar[0].name;
  } else if (options.artists) {
    author = options.artists[0].name;
  } else {
    author = '未知作者';
  }
  return author;
}

function getDuration(options) {
  let dt = options.dt || options.duration;
  return dt / 1000;
}

function getPicUrl(options) {
  let picUrl = '';
  if (options.al) {
    picUrl = options.al.picUrl;
  } else if (options.album && options.album.artist) {
    picUrl = options.album.artist.img1v1Url;
  }
  return picUrl;
}

// 创建歌曲
export function createSong(options) {
  return new Song({
    id: options.id,
    name: options.name,
    author: getAuthor(options),
    duration: getDuration(options),
    url: `https://music.163.com/song/media/outer/url?id=${options.id}.mp3`,
    picUrl: getPicUrl(options)
  });
}
